package fr.iut2.sil4.data;

import java.io.Serializable;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.torque.TorqueException;
import org.apache.torque.map.TableMap;
import org.apache.torque.om.ComboKey;
import org.apache.torque.om.DateKey;
import org.apache.torque.om.NumberKey;
import org.apache.torque.om.ObjectKey;
import org.apache.torque.om.SimpleKey;
import org.apache.torque.om.StringKey;
import org.apache.torque.om.BooleanKey;
import org.apache.torque.om.Persistent;
import org.apache.torque.om.ColumnAccessByName;
import org.apache.torque.criteria.Criteria;
import org.apache.torque.util.Transaction;
import org.apache.commons.lang.ObjectUtils;


/**
 * Absence Table
 *
 * This class was autogenerated by Torque on:
 *
 * [Mon Jan 20 15:56:23 CET 2014]
 *
 * You should not use this class directly.  It should not even be
 * extended; all references should be to Absence
 */
public abstract class BaseAbsence 
    implements Persistent, Serializable, ColumnAccessByName
{
    /** Serial version */
    private static final long serialVersionUID = 1390229783038L;



    /** Absence's Id */
    private int absenceId = 0;

    /** The student who's absent */
    private int studentId = 0;

    /** The cours the student's absent in */
    private int coursId = 0;

    /** Whether this object was modified after loading or after last save. */
    private boolean modified = true;

    /** 
     * Whether this object was loaded from the database or already saved 
     * (false) or whether it is not yet in the database(true).
     */
    private boolean isNew = true;

    /** Flag which indicates whether this object is currently saving. */
    private boolean saving = false;

    /** 
     * Flag which indicates whether this object is currently loaded
     * from the database. 
     */
    private boolean loading = false;

    /** 
     * Flag which indicates whether this object was deleted from the database.
     * Note that this flags does not always display the current database state,
     * there is no magical connection between this flag and the database.
     */
    private boolean deleted = false;

    /** Defines the aStudent field. */
    private Student aStudent = null;

    /** Defines the aCours field. */
    private Cours aCours = null;





    /**
     * Get the value of absenceId.
     * The field is described as follows: 
     * Absence's Id
     *
     * @return int
     */
    public int getAbsenceId() 
    {
        
        return absenceId;
    }

    /**
     * Set the value of absenceId.
     * The field is described as follows: 
     * Absence's Id
     *
     * @param v new value
     */
    public void setAbsenceId(int v)
    {
        if (this.absenceId != v)
        {
            setModified(true);
        }

        this.absenceId = v;


    }
    
    /**
     * Get the value of studentId.
     * The field is described as follows: 
     * The student who's absent
     *
     * @return int
     */
    public int getStudentId() 
    {
        
        return studentId;
    }

    /**
     * Set the value of studentId.
     * The field is described as follows: 
     * The student who's absent
     *
     * @param v new value
     */
    public void setStudentId(int v)
    {
        if (this.studentId != v)
        {
            setModified(true);
        }

        this.studentId = v;
        if (aStudent != null && aStudent.getStudentId() != v)
        {
            aStudent = null;
        }


    }
    
    /**
     * Get the value of coursId.
     * The field is described as follows: 
     * The cours the student's absent in
     *
     * @return int
     */
    public int getCoursId() 
    {
        
        return coursId;
    }

    /**
     * Set the value of coursId.
     * The field is described as follows: 
     * The cours the student's absent in
     *
     * @param v new value
     */
    public void setCoursId(int v)
    {
        if (this.coursId != v)
        {
            setModified(true);
        }

        this.coursId = v;
        if (aCours != null && aCours.getCoursId() != v)
        {
            aCours = null;
        }


    }
    

    /**
     * Returns whether the object has ever been saved.  This will
     * be false, if the object was retrieved from storage or was created
     * and then saved.
     *
     * @return true, if the object has never been persisted.
     */
    public boolean isNew()
    {
        return isNew;
    }

    /**
     * Sets whether the object has ever been saved.
     *
     * @param isNew true if the object has never been saved, false otherwise.
     */
    public void setNew(boolean isNew)
    {
        this.isNew = isNew;
    }

    /**
     * Returns whether the object has been modified.
     *
     * @return True if the object has been modified.
     */
    public boolean isModified()
    {
        return modified;
    }

    /**
     * Sets whether the object has been modified.
     *
     * @param modified true if the object has been modified, false otherwise.
     */
    public void setModified(boolean modified)
    {
        this.modified = modified;
    }

    /**
     * Sets the modified state for the object to be false.
     */
    public void resetModified()
    {
        modified = false;
    }


    /**
     * Returns whether this object is currently saving.
     *
     * @return true if this object is currently saving, false otherwise.
     */
    public boolean isSaving()
    {
        return saving;
    }

    /**
     * Sets whether this object is currently saving.
     *
     * @param saving true if this object is currently saving, false otherwise.
     */
    public void setSaving(boolean saving)
    {
        this.saving = saving;
    }


    /**
     * Returns whether this object is currently being loaded from the database.
     *
     * @return true if this object is currently loading, false otherwise.
     */
    public boolean isLoading()
    {
        return loading;
    }

    /**
     * Sets whether this object is currently being loaded from the database.
     *
     * @param loading true if this object is currently loading, false otherwise.
     */
    public void setLoading(boolean loading)
    {
        this.loading = loading;
    }


    /**
     * Returns whether this object was deleted from the database.
     * Note that this getter does not automatically reflect database state,
     * it will be set to true by Torque if doDelete() was called with this 
     * object. Bulk deletes and deletes via primary key do not change
     * this flag. Also, if doDelete() was called on an object which does
     * not exist in the database, the deleted flag is set to true even if
     * it was not deleted.
     *
     * @return true if this object was deleted, false otherwise.
     */
    public boolean isDeleted()
    {
        return deleted;
    }

    /**
     * Sets whether this object was deleted from the database.
     *
     * @param deleted true if this object was deleted, false otherwise.
     */
    public void setDeleted(boolean deleted)
    {
        this.deleted = deleted;
    }


    /**
     * Returns the associated Student object.
     * If it was not retrieved before, the object is retrieved from
     * the database
     *
     * @return the associated Student object
     * @throws TorqueException when reading from the database fails.
     */
    public Student getStudent()
        throws TorqueException
    {
        if (aStudent == null && this.studentId != 0)
        {
            aStudent = fr.iut2.sil4.data.StudentPeer.retrieveByPK(SimpleKey.keyFor(this.studentId));
        }
        return aStudent;
    }

    /**
     * Return the associated Student object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Student object
     * @throws TorqueException
     */
    public Student getStudent(Connection connection)
        throws TorqueException
    {
        if (aStudent == null && this.studentId != 0)
        {
            aStudent = fr.iut2.sil4.data.StudentPeer.retrieveByPK(SimpleKey.keyFor(this.studentId), connection);
        }
        return aStudent;
    }


    /**
     * Declares an association between this object and a Student object
     *
     * @param v Student
     */
    public void setStudent(Student v)
    {
        if (v == null)
        {
            setStudentId(0);
        }
        else
        {
            setStudentId(v.getStudentId());
        }
        aStudent = v;
    }
    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setStudentKey(ObjectKey key) throws TorqueException
    {
       
        setStudentId(((NumberKey) key).intValue());
     }

    /**
     * Returns the associated Cours object.
     * If it was not retrieved before, the object is retrieved from
     * the database
     *
     * @return the associated Cours object
     * @throws TorqueException when reading from the database fails.
     */
    public Cours getCours()
        throws TorqueException
    {
        if (aCours == null && this.coursId != 0)
        {
            aCours = fr.iut2.sil4.data.CoursPeer.retrieveByPK(SimpleKey.keyFor(this.coursId));
        }
        return aCours;
    }

    /**
     * Return the associated Cours object
     * If it was not retrieved before, the object is retrieved from
     * the database using the passed connection
     *
     * @param connection the connection used to retrieve the associated object
     *        from the database, if it was not retrieved before
     * @return the associated Cours object
     * @throws TorqueException
     */
    public Cours getCours(Connection connection)
        throws TorqueException
    {
        if (aCours == null && this.coursId != 0)
        {
            aCours = fr.iut2.sil4.data.CoursPeer.retrieveByPK(SimpleKey.keyFor(this.coursId), connection);
        }
        return aCours;
    }


    /**
     * Declares an association between this object and a Cours object
     *
     * @param v Cours
     */
    public void setCours(Cours v)
    {
        if (v == null)
        {
            setCoursId(0);
        }
        else
        {
            setCoursId(v.getCoursId());
        }
        aCours = v;
    }
    /**
     * Provides convenient way to set a relationship based on a
     * ObjectKey, for example
     * <code>bar.setFooKey(foo.getPrimaryKey())</code>
     *
     */
    public void setCoursKey(ObjectKey key) throws TorqueException
    {
       
        setCoursId(((NumberKey) key).intValue());
     }






    private static final List<String> FIELD_NAMES;

    static
    {
        List<String> fieldNames
                = new ArrayList<String>();
        fieldNames.add("AbsenceId");
        fieldNames.add("StudentId");
        fieldNames.add("CoursId");
        FIELD_NAMES = Collections.unmodifiableList(fieldNames);
    }

    /**
     * Generate a list of field names.
     *
     * @return a list of field names
     */
    public static List<String> getFieldNames()
    {
        return FIELD_NAMES;
    }

    /**
     * Retrieves a field from the object by field (Java) name passed in as a String.
     *
     * @param name field name
     * @return value
     */
    public Object getByName(String name)
    {
        if (name.equals("AbsenceId"))
        {
            return new Integer(getAbsenceId());
        }
        if (name.equals("StudentId"))
        {
            return new Integer(getStudentId());
        }
        if (name.equals("CoursId"))
        {
            return new Integer(getCoursId());
        }
        return null;
    }

    /**
     * Set a field in the object by field (Java) name.
     *
     * @param name field name
     * @param value field value
     * @return True if value was set, false if not (invalid name / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    public boolean setByName(String name, Object value)
        throws TorqueException, IllegalArgumentException
    {
        if (name.equals("AbsenceId"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setAbsenceId(((Integer) value).intValue());
            return true;
        }
        if (name.equals("StudentId"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setStudentId(((Integer) value).intValue());
            return true;
        }
        if (name.equals("CoursId"))
        {
            if (value == null || ! (Integer.class.isInstance(value)))
            {
                throw new IllegalArgumentException("setByName: value parameter was null or not an Integer object.");
            }
            setCoursId(((Integer) value).intValue());
            return true;
        }
        return false;
    }

    /**
     * Retrieves a field from the object by name passed in
     * as a String.  The String must be one of the static
     * Strings defined in this Class' Peer.
     *
     * @param name peer name
     * @return value
     */
    public Object getByPeerName(String name)
    {
        if (name.equals(fr.iut2.sil4.data.AbsencePeer.ABSENCE_ID))
        {
            return new Integer(getAbsenceId());
        }
        if (name.equals(fr.iut2.sil4.data.AbsencePeer.STUDENT_ID))
        {
            return new Integer(getStudentId());
        }
        if (name.equals(fr.iut2.sil4.data.AbsencePeer.COURS_ID))
        {
            return new Integer(getCoursId());
        }
        return null;
    }

    /**
     * Set field values by Peer Field Name
     *
     * @param name field name
     * @param value field value
     * @return True if value was set, false if not (invalid name / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    public boolean setByPeerName(String name, Object value)
        throws TorqueException, IllegalArgumentException
    {
        if (fr.iut2.sil4.data.AbsencePeer.ABSENCE_ID.getSqlExpression().equals(name))
        {
            return setByName("AbsenceId", value);
        }
        if (fr.iut2.sil4.data.AbsencePeer.STUDENT_ID.getSqlExpression().equals(name))
        {
            return setByName("StudentId", value);
        }
        if (fr.iut2.sil4.data.AbsencePeer.COURS_ID.getSqlExpression().equals(name))
        {
            return setByName("CoursId", value);
        }
        return false;
    }

    /**
     * Retrieves a field from the object by Position as specified
     * in the xml schema.  Zero-based.
     *
     * @param pos position in xml schema
     * @return value
     */
    public Object getByPosition(int pos)
    {
        if (pos == 0)
        {
            return new Integer(getAbsenceId());
        }
        if (pos == 1)
        {
            return new Integer(getStudentId());
        }
        if (pos == 2)
        {
            return new Integer(getCoursId());
        }
        return null;
    }

    /**
     * Set field values by its position (zero based) in the XML schema.
     *
     * @param position The field position
     * @param value field value
     * @return True if value was set, false if not (invalid position / protected field).
     * @throws IllegalArgumentException if object type of value does not match field object type.
     * @throws TorqueException If a problem occurs with the set[Field] method.
     */
    public boolean setByPosition(int position, Object value)
        throws TorqueException, IllegalArgumentException
    {
        if (position == 0)
        {
            return setByName("AbsenceId", value);
        }
        if (position == 1)
        {
            return setByName("StudentId", value);
        }
        if (position == 2)
        {
            return setByName("CoursId", value);
        }
        return false;
    }



    /**
     * Stores an object in the database.  If the object is new,
     * it is inserted; otherwise an update is performed.
     *
     * @param toSave the object to be saved, not null.
     *
     * @throws TorqueException if an error occurs during saving.
     */
    public void save() throws TorqueException
    {
        save(AbsencePeer.DATABASE_NAME);
    }

    /**
     * Stores an object in the database.  If the object is new,
     * it is inserted; otherwise an update is performed.
     *
     * @param toSave the object to be saved, not null.
     * @param dbName the name of the database to which the object
     *        should be saved.
     *
     * @throws TorqueException if an error occurs during saving.
     */
    public void save(String dbName) 
            throws TorqueException
    {
        Connection con = null;
        try
        {
            con = Transaction.begin(dbName);
            save(con);
            Transaction.commit(con);
        }
        catch(TorqueException e)
        {
            Transaction.safeRollback(con);
            throw e;
        }
    }

    /**
     * Stores an object in the database.  If the object is new,
     * it is inserted; otherwise an update is performed.  This method
     * is meant to be used as part of a transaction, otherwise use
     * the save() method and the connection details will be handled
     * internally.
     *
     * @param toSave the object to be saved, not null.
     * @param con the connection to use for saving the object, not null.
     *
     * @throws TorqueException if an error occurs during saving.
     */
    public void save(Connection con) 
            throws TorqueException
    {
        if (isSaving())
        {
            return;
        }
        try
        {
            setSaving(true);
            // If this object has been modified, then save it to the database.
            if (isModified())
            {
                if (isNew())
                {
                    AbsencePeer.doInsert((Absence) this, con);
                    setNew(false);
                }
                else
                {
                    AbsencePeer.doUpdate((Absence) this, con);
                }
            }

        }
        finally
        {
            setSaving(false);
        }
    }




    /**
     * Set the PrimaryKey using ObjectKey.
     *
     * @param key absenceId ObjectKey
     */
    public void setPrimaryKey(ObjectKey key)
        
    {
        setAbsenceId(((NumberKey) key).intValue());
    }

    /**
     * Set the PrimaryKey using a String.
     *
     * @param key
     */
    public void setPrimaryKey(String key) 
    {
        setAbsenceId(Integer.parseInt(key));
    }


    /**
     * returns an id that differentiates this object from others
     * of its class.
     */
    public ObjectKey getPrimaryKey()
    {
        return SimpleKey.keyFor(getAbsenceId());
    }


    /**
     * Returns an id that differentiates the referenced Student object
     * from all other Student objects.
     *
     * @return an ObjectKey.
     */
    public ObjectKey getForeignKeyForStudent()
    {
        int foreignKey = getStudentId();
        if (foreignKey == 0)
        {
            return SimpleKey.keyFor((Number) null);
        }
        return SimpleKey.keyFor(foreignKey);
    }
    /**
     * Returns an id that differentiates the referenced Cours object
     * from all other Cours objects.
     *
     * @return an ObjectKey.
     */
    public ObjectKey getForeignKeyForCours()
    {
        int foreignKey = getCoursId();
        if (foreignKey == 0)
        {
            return SimpleKey.keyFor((Number) null);
        }
        return SimpleKey.keyFor(foreignKey);
    }

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
     */
    public Absence copy() throws TorqueException
    {
        return copy(true);
    }

    /**
     * Makes a copy of this object using a connection.
     * It creates a new object filling in the simple attributes.
     * It then fills all the association collections and sets the
     * related objects to isNew=true.
     *
     * @param con the database connection to read associated objects.
     */
    public Absence copy(Connection con) throws TorqueException
    {
        return copy(true, con);
    }

    /**
     * Makes a copy of this object.
     * It creates a new object filling in the simple attributes.
     * If the parameter deepcopy is true, it then fills all the
     * association collections and sets the related objects to
     * isNew=true.
     *
     * @param deepcopy whether to copy the associated objects.
     */
    public Absence copy(boolean deepcopy) throws TorqueException
    {
        Absence absence = new Absence();

        return copyInto(absence, deepcopy);
    }

    /**
     * Makes a copy of this object using connection.
     * It creates a new object filling in the simple attributes.
     * If the parameter deepcopy is true, it then fills all the
     * association collections and sets the related objects to
     * isNew=true.
     *
     * @param deepcopy whether to copy the associated objects.
     * @param con the database connection to read associated objects.
     */
    public Absence copy(boolean deepcopy, Connection con) throws TorqueException
    {
        Absence absence = new Absence();

        return copyInto(absence, deepcopy, con);
    }
  
    /**
     * Fills the copyObj with the contents of this object.
     * The associated objects are also copied and treated as new objects.
     *
     * @param copyObj the object to fill.
     */
    public Absence copyInto(Absence copyObj) throws TorqueException
    {
        return copyInto(copyObj, true);
    }

    /**
     * Fills the copyObj with the contents of this object using connection.
     * The associated objects are also copied and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param con the database connection to read associated objects.
     */
    public Absence copyInto(Absence copyObj, Connection con) throws TorqueException
    {
        return copyInto(copyObj, true, con);
    }
  
    /**
     * Fills the copyObj with the contents of this object.
     * If deepcopy is true, The associated objects are also copied
     * and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param deepcopy whether the associated objects should be copied.
     */
    protected Absence copyInto(Absence copyObj, boolean deepcopy) throws TorqueException
    {
        copyObj.setAbsenceId(0);
        copyObj.setStudentId(studentId);
        copyObj.setCoursId(coursId);

        if (deepcopy)
        {
        }
        return copyObj;
    }
        
    
    /**
     * Fills the copyObj with the contents of this object using connection.
     * If deepcopy is true, The associated objects are also copied
     * and treated as new objects.
     *
     * @param copyObj the object to fill.
     * @param deepcopy whether the associated objects should be copied.
     * @param con the database connection to read associated objects.
     */
    public Absence copyInto(Absence copyObj, boolean deepcopy, Connection con) throws TorqueException
    {
        copyObj.setAbsenceId(0);
        copyObj.setStudentId(studentId);
        copyObj.setCoursId(coursId);

        if (deepcopy)
        {
        }
        return copyObj;
    }

    /** The Peer class */
    private static final fr.iut2.sil4.data.AbsencePeer peer
            = new fr.iut2.sil4.data.AbsencePeer();

    /**
     * returns a peer instance associated with this om.  Since Peer classes
     * are not to have any instance attributes, this method returns the
     * same instance for all member of this class. The method could therefore
     * be static, but this would prevent one from overriding the behavior.
     */
    public fr.iut2.sil4.data.AbsencePeer getPeer()
    {
        return peer;
    }

    /**
     * Retrieves the TableMap object related to this Table data without
     * compiler warnings of using getPeer().getTableMap().
     *
     * @return The associated TableMap object.
     */
    public TableMap getTableMap() throws TorqueException
    {
        return fr.iut2.sil4.data.AbsencePeer.getTableMap();
    }



    public String toString()
    {
        StringBuffer str = new StringBuffer();
        str.append("Absence:\n");
        str.append("absenceId = ")
           .append(getAbsenceId())
           .append("\n");
        str.append("studentId = ")
           .append(getStudentId())
           .append("\n");
        str.append("coursId = ")
           .append(getCoursId())
           .append("\n");
        return(str.toString());
    }

    /**
     * Compares the primary key of this instance with the key of another.
     *
     * @param toCompare The object to compare to.
     * @return Whether the primary keys are equal and the object have the
     *         same class.
     */
    public boolean equals(Object toCompare)
    {
        if (toCompare == null)
        {
            return false;
        }
        if (this == toCompare)
        {
            return true;
        }
        if (!getClass().equals(toCompare.getClass()))
        {
            return false;
        }
        Absence other = (Absence) toCompare;
        if (getPrimaryKey() == null || other.getPrimaryKey() == null)
        {
            return false;
        }
        return getPrimaryKey().equals(other.getPrimaryKey());
    }

    /**
     * If the primary key is not <code>null</code>, return the hashcode of the
     * primary key.  Otherwise calls <code>Object.hashCode()</code>.
     *
     * @return an <code>int</code> value
     */
    public int hashCode()
    {
        ObjectKey ok = getPrimaryKey();
        if (ok == null)
        {
            return super.hashCode();
        }

        return ok.hashCode();
    }



    /**
     * Compares the content of this object to another object
     *
     * @param toCompare The object to compare to.
     * @return true if all of the columns in the other object have 
     *         the same value as the objects in this class.
     */
    public boolean valueEquals(Absence toCompare)
    {
        if (toCompare == null)
        {
            return false;
        }
        if (this == toCompare)
        {
            return true;
        }
        if (this.absenceId != toCompare.getAbsenceId())
        {
            return false;
        }
        if (this.studentId != toCompare.getStudentId())
        {
            return false;
        }
        if (this.coursId != toCompare.getCoursId())
        {
            return false;
        }
        return true;
    }



}
